package com.thoughtworks.jspecsupport;

import com.thoughtworks.jspecsupport.settings.InvalidSettingsException;
import com.thoughtworks.jspecsupport.settings.SettingsValidator;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.*;

public class RunnerGeneratorTest {
    private String testFileSource1 = "load('jspec.js');\n" +
                                     "load('calendar.js');\n" +
                                     "JSpec.describe(function() {}});";

    private String testFileSource2 = "load('jspec.js');\n" +
                                     "load('daydetail.js');\n" +
                                     "JSpec.describe(function() {}});";

    private List<String> testFileSources = new ArrayList<String>() {{
        add(testFileSource1);
        add(testFileSource2);
    }};

    private String testFilePath1 = "/path/to/spec.calendar.js";
    private String testFilePath2 = "/path/to/spec.daydetail.js";

    private List<String> testFilePaths = new ArrayList<String>() {{
        add(testFilePath1);
        add(testFilePath2);
    }};

    private String prodJsFolder = "/path/to/prod/jsfolder/";
    private String testLibFolder = "/path/to/testLib/jsfolder/";

    @Test
    public void should_generate_test_runner_according_given_input() {
        RunnerGenerator generator = new RunnerGenerator(new DummayFileFinder(), new DummaySettingsValidator(true));
        String generated = generator.generate(testFileSources, testFilePaths, prodJsFolder, testLibFolder);

        String expected = "load('/path/to/prod/jsfolder/jspec.js');\n" +
                          "load('/path/to/prod/jsfolder/calendar.js');\n" +
                          "load('/path/to/prod/jsfolder/daydetail.js');\n" +
                          "JSpec.exec('/path/to/spec.calendar.js').exec('/path/to/spec.daydetail.js').run({reporter: JSpec.reporters.Terminal}).report();";
        assertEquals(expected, generated);
    }

    class DummayFileFinder implements FileFinder {

        public String findFileUnderFolder(String folder, String file) {
            return folder + file;
        }
    }

    class DummaySettingsValidator extends SettingsValidator {

        private boolean valid;

        public DummaySettingsValidator(boolean valid) {
            this.valid = valid;
        }

        @Override
        public void validate(String prodJsFolder, String testLibFolder) {
            if (!valid) throw new InvalidSettingsException("Fake errors throwen from DummaySettingsValidator.");            
        }

    }
}
